import os

from fastapi import Depends, FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi_pagination import add_pagination
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware

from forward.api.admin.routers import (
    article,
    channel,
    common,
    decorate,
    lucky,
    monitor,
    setting,
    system,
    user,
)

# from .generator.routers import gen
from forward.api.admin.routers.merchant import merchant
from forward.api.admin.routers.operator import operator
from forward.api.admin.routers.salesman import salesman
from forward.config import get_settings
from forward.dependencies.verify import admin_verify_token, verify_show_mode
from forward.exceptions.global_exc import configure_exception
from forward.middlewares.init_cors import init_cors_middleware
from forward.middlewares.init_time_out import init_timeout_middleware


def configure_middleware(app: FastAPI):
    """配置中间件"""

    app.add_middleware(ProxyHeadersMiddleware)
    init_cors_middleware(app)
    init_timeout_middleware(app)


def configure_admin_router(app: FastAPI, prefix=""):
    """配置后台路由"""

    settings = get_settings()
    # 后台依赖
    admin_deps = [Depends(admin_verify_token)]
    if settings.disallow_modify:
        admin_deps.append(Depends(verify_show_mode))
    # admin
    app.include_router(user.router, prefix=prefix, dependencies=admin_deps)
    app.include_router(common.router, prefix=prefix, dependencies=admin_deps)
    app.include_router(system.router, prefix=prefix, dependencies=admin_deps)
    app.include_router(monitor.router, prefix=prefix, dependencies=admin_deps)
    app.include_router(setting.router, prefix=prefix, dependencies=admin_deps)
    app.include_router(article.router, prefix=prefix, dependencies=admin_deps)
    app.include_router(decorate.router, prefix=prefix, dependencies=admin_deps)
    app.include_router(channel.router, prefix=prefix, dependencies=admin_deps)
    # gen
    # app.include_router(gen.router, prefix=prefix, dependencies=admin_deps)
    app.include_router(
        lucky.activity, prefix=prefix + "/activity", dependencies=admin_deps
    )
    app.include_router(
        lucky.activity_order, prefix=prefix + "/activity", dependencies=admin_deps
    )
    app.include_router(lucky.code, prefix=prefix + "/activity", dependencies=admin_deps)
    app.include_router(
        lucky.code_batch, prefix=prefix + "/activity", dependencies=admin_deps
    )
    app.include_router(lucky.draw, prefix=prefix + "/activity", dependencies=admin_deps)
    app.include_router(
        lucky.prize, prefix=prefix + "/activity", dependencies=admin_deps
    )
    app.include_router(merchant, prefix=prefix, dependencies=admin_deps)
    app.include_router(operator, prefix=prefix, dependencies=admin_deps)
    app.include_router(salesman, prefix=prefix, dependencies=admin_deps)


def configure_static(app: FastAPI):
    """配置静态资源"""

    settings = get_settings()
    # 上传路径创建
    if not os.path.exists(settings.upload_directory):
        os.makedirs(settings.upload_directory)
    # 上传路径配置
    app.mount(
        settings.upload_prefix,
        StaticFiles(directory=settings.upload_directory),
        name="upload",
    )
    # 静态资源路径配置
    if settings.enabled_static:
        app.mount(
            settings.static_path,
            StaticFiles(directory=settings.static_directory),
            name="static",
        )
    print("static_path", settings.static_directory)
    print("upload", settings.upload_directory)


def create_admin() -> FastAPI:
    """创建FastAPI后台应用,并初始化"""

    app = FastAPI()
    configure_exception(app)
    configure_admin_router(app)
    configure_middleware(app)
    # configure_static(app)
    add_pagination(app)
    return app
